/**
 * @description order 订单处理
 * @author 李哲
 */
const router = require('koa-router')()
const { successModel, errnoModel } = require('../res-model/index');
const loginCheck = require('../middleware/loginCheck');
const { createOrder, getOrderList } = require('../controller/order');

router.prefix('/api/order')
router.post('/', loginCheck, async (ctx, next) => {
    const { username } = ctx.session.userInfor;
    const data = ctx.request.body
    try {
        const newOrder = await createOrder(username, data);
        ctx.body = new successModel(newOrder)
    } catch (err) {
        console.error(err);
        ctx.body = new errnoModel('创建订单失败');
    }
})
router.get('/', loginCheck, async (ctx, next) => {
    const { username } = ctx.session.userInfor
    const newOrder = await getOrderList(username);
    ctx.body = new successModel(newOrder)
})
module.exports = router